import { createRouter, createWebHistory } from 'vue-router'

import Test from '@/views/test/test.vue'
import { useUserStore } from '@/stores/modules/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
       meta: { title: '登录' }
    },
    {
      path: '/test',
      name: 'test',
      component: Test,
    },
     {
      path: '/',
      component: ()=>import('@/views/layout/index.vue'),
      //   children: [
      //   { path: '/home', component: () => import('@/views/home/index.vue') },
      //   { path: '/article', component: () => import('@/views/article/index.vue') },
      //   { path: '/notify', component: () => import('@/views/notify/index.vue') },
      //   { path: '/user', component: () => import('@/views/user/index.vue') }
      // ]
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue'),
          meta: { title: '健康百科' }
        },
        {
          path: '/notify',
          component: () => import('@/views/notify/index.vue'),
          meta: { title: '消息通知' }
        },
        {
          path: '/user',
          component: () => import('@/views/user/index.vue'),
          meta: { title: '个人中心' }
        }
      ]
    },
      {
      path: '/user/patient',
      component: () => import('@/views/user/PatientInfo.vue'),
      meta: { title: '家庭档案' }
    }
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue'),
    // },
  ],
})
// 访问权限控制
router.beforeEach((to, from) => {
  document.title=`在线问诊-${to.meta.title}`
  // 用户仓库
  const store = useUserStore()
  // 不需要登录的页面，白名单
  const wihteList = ['/login']
  // 如果没有登录且不在白名单内，去登录
  if (!store.user?.token && !wihteList.includes(to.path)) return '/login'
  // 否则不做任何处理
})
export default router
